package com.se.common;

import org.json.JSONObject;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.se.common.Constant.*;

public class User implements Serializable {
    private String userName;
    private String password;
    private String status;
    //记录我应该提醒谁（我给谁发了新消息
    private final List<String> notifyList = new ArrayList<>();
    //仅在发送消息时使用，用于指示当前用户是否有未读消息
    private boolean notify;
    @Serial
    private static final long serialVersionUID = 1L;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void setNotify(boolean notify) {
        this.notify = notify;
    }

    public boolean isNotified() {
        return notify;
    }

    public void addNotify(String username) {
        notifyList.add(username);
    }

    public void rmNotify(String username) {
        notifyList.remove(username);
    }

    public List<String> getNotifyList() {
        return notifyList;
    }

    public User(String userName, String password){
        this.userName = userName;
        this.password = password;
    }

    public String toString() {
        JSONObject json = new JSONObject();
        json.put(USERNAME, userName);
        json.put(PASSWORD, password);
        json.put(STATUS, status);
        json.put(NOTIFY, notify);
        return json.toString();
    }

    public User(){
        this(null, null);
    }

    public String getStringNotifyList() {
        StringBuilder builder = new StringBuilder();
        for(String i : notifyList) {
            builder.append(i);
            builder.append(" ");
        }
        return builder.toString();
    }

    public void setNotifyList(String list) {
        String[] users = list.split(" ");
        notifyList.addAll(Arrays.asList(users));
    }

    public void setUser(User newUser) {
        this.userName = newUser.getUserName();
        this.password = newUser.getPassword();
        this.status = newUser.getStatus();
        this.notifyList.clear();
        this.notifyList.addAll(newUser.getNotifyList());
        this.notify = newUser.isNotified();
    }
}
